package com.douding.server.service;

import com.douding.server.domain.Teacher;
import com.douding.server.domain.TeacherExample;
import com.douding.server.dto.TeacherDto;
import com.douding.server.dto.PageDto;
import com.douding.server.mapper.TeacherMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;
import com.douding.server.util.ValidatorUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class TeacherService {

    @Resource
    private TeacherMapper teacherMapper;


    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        //根据传入的PageDto对象获取分页的页数和一页的数据多少
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        TeacherExample teacherExample = new TeacherExample();
        //查询到对应的teacher对象
        List<Teacher> teacherList = teacherMapper.selectByExample(teacherExample);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        pageDto.setTotal(pageInfo.getTotal());
        //将TeacherList对应内容赋值给一个TeacherDtoList
        List<TeacherDto> teacherDtoList = CopyUtil.copyList(teacherList, TeacherDto.class);
        pageDto.setList(teacherDtoList);
    }

    /**
     * 保存或修改对象
     * @param teacherDto 教师对象
     */
    public void save(TeacherDto teacherDto) {
        //验证传入信息是否健全
        ValidatorUtil.require(teacherDto.getName(), "教师姓名");
        ValidatorUtil.require(teacherDto.getNickname(), "教师昵称");
        ValidatorUtil.require(teacherDto.getPosition(), "教师名称");
        ValidatorUtil.require(teacherDto.getMotto(), "教师座右铭");
        ValidatorUtil.require(teacherDto.getIntro(), "教师简介");
        //将TeacherDto对象对应内容复制到一个Teacher对象中
        Teacher teacher = CopyUtil.copy(teacherDto, Teacher.class);

        //判断是新增 还是修改
        if (StringUtils.isEmpty(teacherDto.getId())) {
            this.insert(teacher);
        } else {
            this.update(teacher);
        }

    }

    //新增数据
    private void insert(Teacher teacher) {
        //给Teacher对象设置id
        teacher.setId(UuidUtil.getShortUuid());
        //增加对象
        teacherMapper.insert(teacher);
    }

    //更新数据
    private void update(Teacher teacher) {
        //修改对象
        teacherMapper.updateByPrimaryKey(teacher);
    }

    public void delete(String id) {
        //根据id删除对象
        teacherMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询所有教师信息
     * @return List<TeacherDto> 教师List
     */
    public List<TeacherDto> all() {
        //创建teacher对象
        TeacherExample teacherExample = new TeacherExample();
        //查询到对应的teacher对象
        List<Teacher> teacherList = teacherMapper.selectByExample(teacherExample);
        //调用Copy工具类的方法给TeacherDtoList赋值
        List<TeacherDto> teacherDtoList = CopyUtil.copyList(teacherList, TeacherDto.class);
        return teacherDtoList;
    }


    /**
     * 查找
     * @param id 教师id
     * @return TeacherDto 教师信息
     */
    public TeacherDto findById(String id) {
        //验证传入信息是否健全
        ValidatorUtil.require(id, "教师id");
        //根据id查询教师
        Teacher teacher = teacherMapper.selectByPrimaryKey(id);
        //使用CopyUtils工具类给TeacherDto属性赋值
        TeacherDto teacherDto = CopyUtil.copy(teacher, TeacherDto.class);
        return teacherDto;
    }
}//end class
